layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var pid=Feng.getUrlParam("pid");
    var id=Feng.getUrlParam("id");
    /**
     * 用户表管理
     */
    var Customer = {
        tableId: "customerTable"
    };

    /**
     * 初始化表格的列
     */
    Customer.initColumn = function () {
        return [[
            {type: 'radio',},
            {field: 'id', hide: true, title: ''},
            {field: 'mobile', sort: true, title: '手机号'},
            {toolbar: '#imgurl', sort: true, title: '头像' },
            {field: 'nickname', sort: true, title: '昵称'},
            {field: 'superior', sort: true, title: '上级领导昵称',templet:function (data) {
                    return data.superior
                }},
            {field: 'support', sort: true, title: '帮扶领导昵称',templet:function (data) {
                    return data.support
                }},
           /* {field: 'sex', sort: true, title: '角色'},*/
            /*{toolbar: '#role', sort: true, title: '角色' },*/
            {field: 'money', sort: true, title: '余额'},
           /* {field: 'role', toolbar: '#role', sort: true, title: '社长/学员'},*/
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Customer.search = function () {
        var queryData = {};
        queryData['mobile'] = $("#mobile").val();
        queryData['nickname'] = $("#nickname").val();
        table.reload(Customer.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    Customer.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/customer/add?pid='+pid
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    Customer.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/customer/edit?id=' + data.id+'&pid='+pid
    };

    /**
     * 导出excel按钮
     */
    Customer.exportExcel = function () {
        var checkRows = table.checkStatus(Customer.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 查看下级
     *
     * @param data 点击按钮时候的行数据
     */
    /*Customer.subordinate = function (data) {
        window.location.href = Feng.ctxPath + '/customer?pid=' + data.id
    };*/
    /**
     * 设置帮扶领导
     *
     * @param data 点击按钮时候的行数据
     */
    Customer.subordinate = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/customer/leader?id="+id+"&leader="+data.id, function (data) {
                Feng.success("设置成功!");
                window.parent.location.reload()
                table.reload(Customer.tableId);
            }, function (data) {
                Feng.error("设置失败!" + data.responseJSON.message + "!");
            });
            /*ajax.set("id", data.id);*/
            ajax.start();
        };
        Feng.confirm("是否设置?", operation);
    };

    /**
     * 查看下级
     *
     * @param data 点击按钮时候的行数据
     */
    Customer.withdraws = function (data) {
        window.location.href = Feng.ctxPath + '/rebate?customerId=' + data.id
    };

    // 修改社长和学院
    form.on('switch(role)', function (obj) {
        console.log("11111")
        var id = obj.elem.value;
        var role = obj.elem.checked ? 1 : 2;
        console.log("222")
        var ajax = new $ax(Feng.ctxPath + "/customer/setHot", function (data) {
            Feng.success("修改成功");
        }, function (data) {
            Feng.error(data.msg());
            table.reload(ShopPro.tableId);
        });
        ajax.set("id", id);
        ajax.set("isHot", role);
        ajax.start();
    });


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Customer.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/customer/delete?pid="+pid, function (data) {
                Feng.success("删除成功!");
                table.reload(Customer.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Customer.tableId,
        url: Feng.ctxPath + '/customer/list?pid='+pid+'&id='+id+'&role=1',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Customer.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Customer.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    Customer.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        Customer.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Customer.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Customer.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            Customer.onDeleteItem(data);
        }else if (layEvent === 'command') {
            Customer.subordinate(data);
        }else if (layEvent === 'record') {
            Customer.withdraws(data);
        }
    });
});
